package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.GetUnassignedListVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "获取待分配的离职成员列表", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "page_id", type = Type.Integer, description = "分页查询，要查询页号，从0开始", required = false),
        @Parameter(name = "page_size", type = Type.Integer, description = "每次返回的最大记录数，默认为1000，最大值为1000", required = false),
        @Parameter(name = "cursor", type = Type.String, description = "分页查询游标，字符串类型，适用于数据量较大的情况，如果使用该参数则无需填写page_id，该参数由上一次调用返回", required = false),
})
@Component
public class GetUnassignedList implements Action {

    @Autowired
    ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {

        Integer page_id = request.getInteger("page_id");
        Integer page_size = request.getInteger("page_size");
        String cursor = request.getString("cursor");

        HashMap<String, Object> params = new HashMap<String, Object>();

        if (page_id != null) {
            params.put("page_id", page_id);
        }
        if (page_size != null) {
            params.put("page_size", page_size);
        }
        if (cursor != null) {
            params.put("cursor", cursor);
        }

        GetUnassignedListVo rsp = externalContactService.getUnassignedList(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
